package com.open.capacity.notice.validator;

import org.springframework.util.ObjectUtils;

import com.open.capacity.notice.provider.core.NoticeProvider;
import com.open.capacity.notice.context.SendMessageJobContext;
import com.open.capacity.notice.enums.RececiverType;

import net.bytebuddy.matcher.ElementMatcher;

/**
 * 微信发送类型只能选择wechat的接收组关联微信号
 */
public class WechatChecker implements ElementMatcher<SendMessageJobContext> {

	@Override
	public boolean matches(SendMessageJobContext contex) {
		if (contex.getMessageTemplate().getSendChannel().equals(NoticeProvider.WECHAT.getCode())) {
			if (ObjectUtils.isEmpty(contex.getMessageRececiverGroupVO())) {
				contex.setMsg("微信接收人为空");
				return false;
			}
			if (contex.getMessageRececiverGroupVO().getRececiverType() != RececiverType.WECHAT) {
				contex.setMsg("微信类型无法选择短信接收组");
				return false;
			}
		}
		return true;
	}

	// 匿名内部类，确保只有在第一次访问时才会被初始化
	private static class Holder {
		private static final WechatChecker INSTANCE = new WechatChecker();
	}

	public static WechatChecker getInstance() {
		return Holder.INSTANCE;
	}

}
